/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateStaff.java
 *
 * Created on Mar 9, 2011, 8:09:31 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.*;
import vn.aptech.project.control.Employee_Detail_BLL;
import vn.aptech.project.model.dto.Employee_Detail_DTO;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Mr.Hiep
 */
public class CreateStaff extends java.awt.Dialog {

    /** Creates new form CreateStaff */
    public CreateStaff(java.awt.Frame parent, boolean modal) throws IOException {
        super(parent, modal);
        initComponents();
        this.setTitle("Create Staff");
        try {
            category_BLL cgb = new category_BLL();
            Vector vt = cgb.selectTypeinfo();
            for (int i = 0; i < vt.size(); i++) {
                String abc = vt.get(i).toString();
                String temp = abc.substring(1, abc.length());
                String temm = temp.substring(0, temp.length() - 1);
                cbbDepart.addItem(temm);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField5 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        bntCreate = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtStName = new javax.swing.JTextField();
        txtContact = new javax.swing.JTextField();
        txtPass = new javax.swing.JPasswordField();
        txtConfirmPass = new javax.swing.JPasswordField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAdd = new javax.swing.JTextArea();
        cbbDepart = new javax.swing.JComboBox();
        bntCancel = new javax.swing.JButton();

        jTextField5.setText("jTextField5");

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        bntCreate.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntCreate.setForeground(new java.awt.Color(0, 0, 255));
        bntCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add_user.png"))); // NOI18N
        bntCreate.setText("Create");
        bntCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCreateActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 18));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Create New Staff");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel2.setForeground(new java.awt.Color(0, 51, 255));
        jLabel2.setText("Staff Name:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel3.setForeground(new java.awt.Color(0, 51, 255));
        jLabel3.setText("Address:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel4.setForeground(new java.awt.Color(0, 51, 255));
        jLabel4.setText("Contact_No:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel5.setForeground(new java.awt.Color(0, 51, 255));
        jLabel5.setText("Department:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel6.setForeground(new java.awt.Color(0, 51, 255));
        jLabel6.setText("PassWord:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel7.setForeground(new java.awt.Color(0, 51, 255));
        jLabel7.setText("Re-enter password:");

        txtStName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));

        txtContact.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        txtContact.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtContactKeyTyped(evt);
            }
        });

        txtPass.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));

        txtConfirmPass.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));

        txtAdd.setColumns(20);
        txtAdd.setRows(5);
        txtAdd.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        jScrollPane1.setViewportView(txtAdd);

        cbbDepart.setForeground(new java.awt.Color(51, 51, 255));
        cbbDepart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Selected--" }));
        cbbDepart.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtStName, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                        .addComponent(txtContact)
                        .addComponent(txtPass)
                        .addComponent(txtConfirmPass)
                        .addComponent(jScrollPane1))
                    .addComponent(cbbDepart, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtStName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel3)
                        .addGap(29, 29, 29))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbbDepart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        bntCancel.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntCancel.setForeground(new java.awt.Color(0, 51, 255));
        bntCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(107, Short.MAX_VALUE)
                .addComponent(bntCreate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bntCancel)
                .addGap(123, 123, 123))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntCreate)
                    .addComponent(bntCancel))
                .addContainerGap())
        );

        add(jPanel1, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void bntCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCreateActionPerformed
        try {
            if (txtStName.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Staff name not blank !!!");
                txtStName.requestFocus();
                return;
            }
            if (txtAdd.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Address not blank !!!");
                txtAdd.requestFocus();
                return;
            }
            if (txtContact.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Contact_No not blank !!!");
                txtContact.requestFocus();
                return;
            }
            if (cbbDepart.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(this, "Department not blank !!!");
                cbbDepart.requestFocus();
                return;
            }
            if (txtPass.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Password not blank !!!");
                txtPass.requestFocus();
                return;
            }
            if (txtConfirmPass.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Re_Password not blank !!!");
                txtConfirmPass.requestFocus();
                return;
            }
            if (!txtConfirmPass.getText().equals(txtPass.getText())) {
                JOptionPane.showMessageDialog(this, "Passwords do not match");
                txtConfirmPass.requestFocus();
                return;
            }
            tbladmin_BLL tab = new tbladmin_BLL();
            Vector vtadmin = tab.selectPWbyUse(txtStName.getText());
            if (vtadmin.isEmpty()) {
                Employee_Detail_BLL edb = new Employee_Detail_BLL();
                Vector vt = edb.SearchEmployee_DetailByName(txtStName.getText());
                if (vt.isEmpty()) {
                    Employee_Detail_DTO edd = new Employee_Detail_DTO();
                    edd.setStaffname(txtStName.getText());
                    edd.setAddr(txtAdd.getText());
                    edd.setContact_no(Integer.parseInt(txtContact.getText()));
                    edd.setDepartment(cbbDepart.getSelectedItem().toString());
                    edd.setPasswordemp(txtPass.getText());
                    Employee_Detail_BLL edb1 = new Employee_Detail_BLL();
                    edb1.insertEmployee_Detail(edd);
                    JOptionPane.showMessageDialog(this, "Successful");
                } else {
                    JOptionPane.showMessageDialog(this, "Staff name already exists");
                    txtStName.requestFocus();
                    return;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Staff name already exists");
                txtStName.requestFocus();
                return;
            }
        } catch (IOException ex) {
            Logger.getLogger(CreateStaff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CreateStaff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CreateStaff.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_bntCreateActionPerformed

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        this.hide();
    }//GEN-LAST:event_bntCancelActionPerformed

    private void txtContactKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtContactKeyTyped
        int key = evt.getKeyChar();
        String st = txtContact.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_txtContactKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    CreateStaff dialog = new CreateStaff(new java.awt.Frame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(CreateStaff.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntCreate;
    private javax.swing.JComboBox cbbDepart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextArea txtAdd;
    private javax.swing.JPasswordField txtConfirmPass;
    private javax.swing.JTextField txtContact;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtStName;
    // End of variables declaration//GEN-END:variables
}
